﻿using System;
using System.Collections.Generic;
using System.Text;
using Database;
using System.Data;

namespace ObjectManager
{
    class DangKyDAO : IDatabase
    {
        private ConnectionDB cnn = null;

        public DangKyDTO DangKyDTO { get; set; }

        private void Initialize()
        {
            cnn = Connection.Connect();
            DangKyDTO = new DangKyDTO();
        }

        public DangKyDAO()
        {
            Initialize();
        }

        public int Insert()
        {
            int MaSV = 0;
            string MaKSV = "";
            MaSV = int.Parse(DangKyDTO.MaSV.Substring(0, DangKyDTO.MaSV.Length - 2));
            MaKSV = DangKyDTO.MaSV.Substring(DangKyDTO.MaSV.Length - 2);
            return cnn.ExecuteProcedure("InsertDangKy", MaSV, MaKSV, DangKyDTO.MaMH, DangKyDTO.MaLop, DangKyDTO.NamHoc, DangKyDTO.HocKy) / 3;
        }

        public int Update()
        {
            throw new NotImplementedException();
        }

        public int Delete()
        {
            return cnn.ExecuteProcedure("DeleteDangKy", DangKyDTO.Ma) / 2;
        }

        public DataTable List(string MaSinhVien)
        {
            return cnn.QueryProcedure("DanhSachDangKy_SinhVien", MaSinhVien);
        }

        public int[] NamHocvaHocKy(string MaSinhVien)
        {
            int[] result = new int[2];
            for (int i = 0; i < 2; i++)
            {
                result[i] = 0;
            }
            DataTable temp = cnn.QueryProcedure("NamHocvaHocKyLonNhat", MaSinhVien);
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                result[0] = int.Parse(temp.Rows[0][0].ToString());
                result[1] = int.Parse(temp.Rows[0][1].ToString());
            }
            return result;
        }

        public DataTable Search(string MaSinhVien, string NamHoc, string HocKy)
        {
            return cnn.QueryProcedure("DanhSanhDangKy_SinhVien_NamHocVaHocKy", MaSinhVien, NamHoc, HocKy);
        }
    }
}
